package com.sprometheus.core.handler;

import cn.hutool.extra.spring.SpringUtil;
import com.sprometheus.core.constants.SPrometheusTitleEnum;
import com.sprometheus.core.constants.Scene;
import com.sprometheus.core.pojo.Exp;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * 异常链路处理工具
 *
 * @author Goslee
 * @date 2024/9/5 下午4:52
 */
@Configuration
public class SPrometheusUtil {

    private static volatile SPrometheusHandler INSTANCE;

    @PostConstruct
    public void init() {
        if (INSTANCE == null) {
            synchronized (SPrometheusHandler.class) {
                if (INSTANCE == null) {
                    INSTANCE = SpringUtil.getBean(SPrometheusHandler.class);
                }
            }
        }
    }


    public static void insert(String msg, SPrometheusTitleEnum titleEnum) {
        Exp req = new Exp();
        req.setMsg(msg);
        req.setTitleEnum(titleEnum);
        INSTANCE.insert(req);
    }

    public static void insert(String msg, SPrometheusTitleEnum titleEnum, String data) {
        Exp req = new Exp();
        req.setMsg(msg);
        req.setTitleEnum(titleEnum);
        req.setReqData(data);
        INSTANCE.insert(req);
    }

    /***
     * 建议用此方式
     * @param req
     */
    public static void insert(Exp req) {
        INSTANCE.insert(req);
    }

    /***
     * 会保留文本原格式推送
     *
     * 例如自定义格式
     *
     * @param req
     */
    public static void insertOrg(Exp req) {
        INSTANCE.insert(req, Scene.ORIGIN_MSG);
    }

}
